package com.example.kingweather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.kingweather.R;
import com.example.kingweather.bean.WeatherBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WeatherGridAdapter extends BaseAdapter {

    private Context context;
    private List<WeatherBean.HeWeather6Bean.DailyForecastBean> list;
    private ViewHolder holder;


    public WeatherGridAdapter(Context context, List<WeatherBean.HeWeather6Bean.DailyForecastBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.grid_view_daily_forcastitem, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.gridDateItem.setText(list.get(i).getDate());

        Glide.with(context).load(context.getResources().getIdentifier("w" + list.get(i).getCond_code_d(), "mipmap", context.getPackageName())).into(holder.gridCondImageDItem);
        Glide.with(context).load(context.getResources().getIdentifier("w" + list.get(i).getCond_code_n(), "mipmap", context.getPackageName())).into(holder.gridCondImageNItem);


        holder.gridCondTxtDItem.setText(list.get(i).getCond_txt_d());
        holder.gridCondTxtNItem.setText(list.get(i).getCond_txt_n());


        holder.gridTmpItem.setText(list.get(i).getTmp_min() + "℃~" + list.get(i).getTmp_max() + "℃");

        holder.itemGridRichu.setText("日出：\n" + list.get(i).getSr());
        holder.itemGridRiluo.setText("日落：\n" + list.get(i).getSs());
        holder.itemGridYuechu.setText("月升：\n" + list.get(i).getSr());
        holder.itemGridYueluo.setText("月落：\n" + list.get(i).getSs());


        holder.gridWindItem.setText(list.get(i).getWind_dir() + "  \n" + list.get(i).getWind_sc() + "级 \n" + list.get(i).getWind_spd() + "km/h");

        holder.gridHumItem.setText(list.get(i).getHum());
        holder.gridPcpnItem.setText(list.get(i).getPcpn());
        holder.gridPopItem.setText(list.get(i).getPop());
        holder.gridPresItem.setText(list.get(i).getPres());
        holder.gridUvIndexItem.setText(list.get(i).getUv_index());
        holder.gridVisItem.setText(list.get(i).getVis());
        if(list.size()-1==i){
            holder.itemView.setVisibility(View.INVISIBLE);
        }

        return view;
    }


    static
    class ViewHolder {
        @BindView(R.id.grid_date_item)
        TextView gridDateItem;
        @BindView(R.id.grid_cond_image_d_item)
        ImageView gridCondImageDItem;
        @BindView(R.id.grid_cond_txt_d_item)
        TextView gridCondTxtDItem;
        @BindView(R.id.grid_cond_image_n_item)
        ImageView gridCondImageNItem;
        @BindView(R.id.grid_cond_txt_n_item)
        TextView gridCondTxtNItem;
        @BindView(R.id.grid_tmp_item)
        TextView gridTmpItem;
        @BindView(R.id.item_grid_richu)
        TextView itemGridRichu;
        @BindView(R.id.item_grid_riluo)
        TextView itemGridRiluo;
        @BindView(R.id.item_grid_yuechu)
        TextView itemGridYuechu;
        @BindView(R.id.item_grid_yueluo)
        TextView itemGridYueluo;
        @BindView(R.id.grid_hum_item)
        TextView gridHumItem;
        @BindView(R.id.grid_pcpn_item)
        TextView gridPcpnItem;
        @BindView(R.id.grid_pop_item)
        TextView gridPopItem;
        @BindView(R.id.grid_pres_item)
        TextView gridPresItem;
        @BindView(R.id.grid_uv_index_item)
        TextView gridUvIndexItem;
        @BindView(R.id.grid_vis_item)
        TextView gridVisItem;
        @BindView(R.id.grid_wind_item)
        TextView gridWindItem;
        @BindView(R.id.grid_view_item)
        View itemView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
